package com.yangrui.service;

import com.yangrui.dao.NameDao;
import com.yangrui.dao.NameDaoImpl;
import com.yangrui.entity.Name;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;


public class NameServiceImpl implements NameService {
    private NameDao nameDao = new NameDaoImpl();

    @Override
    public ArrayList<Name> findAll() {
        return nameDao.findAllWhere("");
    }

    @Override
    public int updatePinyin() {
        //查询所有name_pinyin为空的字段
        ArrayList<Name> nameList = nameDao.findAllWhere("where name_pinyin is null");
        //1.new一个汉语拼音输出格式对象
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        //2.设置格式
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //1).设置声调标识为空
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE); //2).设置大小写转换为大写
        /**
         * 设置每多少条数据开启一条线程 我没有那么多数据 为了显示直观一点我这里设置的是2
         */
        int threadSize = 2;
        // 总数据条数
        int dataSize = nameList.size();
        // 线程数
        int threadNum = dataSize / threadSize + 1;
        // 定义标记,过滤threadNum为整数
        boolean special = dataSize % threadSize == 0;
        // 创建一个线程池
        ExecutorService exec = Executors.newFixedThreadPool(threadNum);
        // 定义一个任务集合
        List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
        Callable<Integer> task = null;
        List<Name> cutList = null;
        // 确定每条线程的数据 拆分nameList
        for (int i = 0; i < threadNum; i++) {
            if (i == threadNum - 1) {
                if (special) {
                    break;
                }
                cutList = nameList.subList(threadSize * i, dataSize);
            } else {
                cutList = nameList.subList(threadSize * i, threadSize * (i + 1));
            }
            List<Name> list = cutList;

            task = () -> {
                for (Name name : list) {
                    char[] chs = name.getName().toCharArray();
                    try {
                        StringBuilder word = new StringBuilder();
                        //寻找所有拼音首字母并拼接
                        for (char c : chs) {
                            String[] hanyuPinyinStringArray =
                                    PinyinHelper.toHanyuPinyinStringArray(c, format);
                            if (hanyuPinyinStringArray.length != 0) { //判断是否为null值,因为字符串中的符号是无法转换的
                                word.append(hanyuPinyinStringArray[0].charAt(0));
                            }
                        }

                        name.setNamePinyin(word.toString());

                    } catch (BadHanyuPinyinOutputFormatCombination e) {
                        throw new RuntimeException(e);
                    }
                    nameDao.update(name);
                }
                System.out.println(Thread.currentThread().getName() + "线程："+list);
                return 1;
            };

            tasks.add(task);
        }
        List<Future<Integer>> results = null;
        try {
            results = exec.invokeAll(tasks);
            for (Future<Integer> future : results) {
                System.out.println(future.get());
            }
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
        exec.shutdown();
        System.out.println("线程执行结束");

//        for (Name name : nameList) {
//            char[] chs = name.getName().toCharArray();
//            try {
//                StringBuilder word = new StringBuilder();
//                //寻找所有拼音首字母并拼接
//                for (char c : chs) {
//                    String[] hanyuPinyinStringArray =
//                            PinyinHelper.toHanyuPinyinStringArray(c, format);
//                    if (hanyuPinyinStringArray.length != 0) { //判断是否为null值,因为字符串中的符号是无法转换的
//                        word.append(hanyuPinyinStringArray[0].charAt(0));
//                    }
//                }
//
//                name.setNamePinyin(word.toString());
//
//            } catch (BadHanyuPinyinOutputFormatCombination e) {
//                throw new RuntimeException(e);
//            }
//            nameDao.update(name);
//        }
        return 1;
    }
}
